import { reactive, ref } from 'vue';
import { routerTo } from '@/utils/jump';
import { auth } from '@/server/auth';
import { userService, userInfo } from '@/service/user';
import { toast } from '@/service/status';
import { assetService, holding_list } from '@/service/asset.js';
import { depositList, withdrawList } from '@/service/deposit.js';

// 登录状态
export const is_login = ref(false);

// 输入框数据
export const account = ref('');              // 账号
export const password = ref('');             // 密码
export const confirmPassword = ref('');      // 确认密码
export const fundPassword = ref('');         // 资金密码
export const confirmFundPassword = ref('');  // 确认资金密码
export const inviteCode = ref('');           // 邀请码
export const agreeToTerms = ref(false);      // 是否同意条款

export const logoutPage = reactive({
    show: false,
    open() {
        logoutPage.show = true;
    },
    close() {
        logoutPage.show = false;
    },
});

export const authService = {
    async login(save=false) {
        let res = await auth.login(account.value, password.value);
        if (res.success) {
            if (res.data.code == 200) {
                is_login.value = true;
                localStorage.setItem('token', res.data.data.accessToken);
                let path = sessionStorage.getItem('active_nav_item');
                if (save) {
                    localStorage.setItem('is_save_password', 'true');
                    localStorage.setItem('account', account.value);
                    localStorage.setItem('password', password.value);
                } else {
                    localStorage.removeItem('is_save_password');
                    localStorage.removeItem('account');
                    localStorage.removeItem('password');
                }
                account.value = '';
                password.value = '';
                routerTo(path ?? '/');
                toast.success('Login successful');
                // 获取用户信息
                userService.getUserInfo();
                // 获取用户持仓列表
                assetService.getStockOrderList();
            } else {
                toast.warning('Login failed! Please check your account and password.');
                console.log('登录失败 需要给用户一定提示才行', res);
            }
        } else {
            toast.warning('Login failed! Please check your account and password.');
            console.log('登录失败 需要给用户一定提示才行', res);
        }
    },
    async register(ok, no) {
        // 发送请求
        let res = await auth.register(account.value, password.value, fundPassword.value, inviteCode.value);
        if (res.success) {
            if (res.data.code == 200) {
                is_login.value = true;
                localStorage.setItem('token', res.data.data.accessToken);
                account.value = '';
                password.value = '';
                // 获取用户信息
                userService.getUserInfo();
                ok();
            } else {
                // console.log('in service 1', res);
                no(res);
            }
        } else {
            // console.log('in service 2', res);
            no(res);
        }
    },
    async logout() {
        // 发送请求
        await auth.logout();
        // 登录状态设置为 false
        is_login.value = false;
        userInfo.account = "";
        userInfo.registerType = 0;
        userInfo.balance = 0;
        userInfo.freeze = 0;
        userInfo.unpaid = 0;
        userInfo.authStatus = 0;
        // 清空持股列表
        holding_list.value = [];
        // 情况提现和充值列表
        depositList.value = [];
        withdrawList.value = [];
        // 删除用户令牌
        localStorage.removeItem('token');
        toast.success('Logout successful');
    },
};
